package cn.marinda.resturants.service.impl;

import cn.marinda.resturants.entity.Identity;
import cn.marinda.resturants.global.Pages;
import cn.marinda.resturants.mapper.IdentityMapper;
import cn.marinda.resturants.service.IdentityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/** 身份服务层实现类
 * @date 2021/12/2
 * @author Marinda
 * @version 1.0
 *
 */
@Service("identityService")
public class IdentityServiceImpl extends ServiceImpl<IdentityMapper,Identity> implements IdentityService {

    /**
     * 选择身份uid
     *
     * @param uid uid
     * @return {@link Identity}
     */
    @Override
    public Identity selectIdentityUid(String uid) {
        return baseMapper.selectById(uid);
    }

    /** 查询身份分类
     *
     * @param pages pages
     * @return 数据
     */
    @Override
    public IPage<Identity> selectIdentityPages(Pages<Identity> pages) {
        IPage<Identity> identityIPage = baseMapper.selectPage(pages,null);
        return identityIPage;
    }

    /**
     * 查询所有身份数据
     *
     * @date 2021/12/2
     */
    @Override
    public List<Identity> queryList() {
        return baseMapper.selectList(null);
    }

    /**
     * 添加身份数据
     *
     * @param identity identity
     * @return 数据处理
     */
    @Override
    public boolean addIdentity(Identity identity) {
        identity.setUid(UUID.randomUUID().toString());
        return baseMapper.insert(identity)>0;
    }

    /**
     * 删除身份数据
     *
     * @param uid uid
     * @return 数据返回
     */
    @Override
    public boolean deleteIdentity(String uid) {
        return baseMapper.deleteById(uid)>0;
    }

    /**
     * 修改身份数据
     *
     * @param identity 身份验证
     * @return 数据返回
     */
    @Override
    public boolean updateIdentity(Identity identity) {
        Identity sqlIdentity = baseMapper.selectById(identity.getUid());
        if(sqlIdentity == null){
            return false;
        }
        identity.setVersion(sqlIdentity.getVersion());
        return lambdaUpdate().eq(Identity::getUid,identity.getUid()).eq(Identity::getVersion,identity.getVersion()).set(Identity::getVersion,identity.getVersion()+1).update(identity);
    }

    /**
     * 查询一个管理员所拥有的全部权限uid
     * 格式一般例如：101,102 逗号隔开
     *
     * @param admin_uid 管理员uid
     * @return 权限uid
     */
    @Override
    public String selectPermissionUid(String admin_uid) {
        return baseMapper.selectPermissionUid(admin_uid);
    }

    /**
     * 查询权限变更时间
     *
     * @param admin_uid 管理员
     * @return 时间
     */
    @Override
    public String selectUpdateTime(String admin_uid) {
        return baseMapper.selectUpdateTime(admin_uid);
    }
}
